import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // 生产环境生成 .gz 文件
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  base: './',
  server: {
    port: 9003,
    proxy: {
      '^/apiProxy': {
        target: 'http://127.0.0.1:8080/leo-erp-admin',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/apiProxy/, '')
      },
      '^/imgProxy/dev': {
        target: 'http://120.77.203.231:9000/dev',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/imgProxy\/dev/, '')
      }
    }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          ckeditor: ['@ckeditor/ckeditor5-build-decoupled-document'],
          wangeditor: ['wangeditor']
        }
      }
    },
    minify: 'terser',
    // 生产环境移除 console
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ['console.log'],
      },
      output: {
        // 去掉注释内容
        comments: true,
      },
    }
  }
})
